package org.crystalrange.documents.web;

import java.util.List;
import java.util.Set;

import org.crystalrange.BaseController;
import org.crystalrange.documents.model.DocumentGroup;
import org.crystalrange.documents.service.DocumentGroupService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import com.googlecode.genericdao.search.Search;

public class DocumentGroupListController extends BaseController {

	protected DocumentGroup selectedDocumentGroup;
	protected DocumentGroupService documentGroupService;
	protected List<DocumentGroup> documentGroups;
	protected Listbox listboxDocumentGroup;
	protected Search searchDocumentGroup;

	public DocumentGroup getSelectedDocumentGroup() {
		return selectedDocumentGroup;
	}

	public void setSelectedDocumentGroup(DocumentGroup selectedDocumentGroup) {
		this.selectedDocumentGroup = selectedDocumentGroup;
	}

	public DocumentGroupService getDocumentGroupService() {
		return documentGroupService;
	}

	public void setDocumentGroupService(
			DocumentGroupService documentGroupService) {
		this.documentGroupService = documentGroupService;
	}

	public List<DocumentGroup> getDocumentGroups() {
		return documentGroups;
	}

	public void setDocumentGroups(List<DocumentGroup> documentGroups) {
		this.documentGroups = documentGroups;
	}

	public void onClick$toolbarbuttonDocumentGroupCreate(Event event)
			throws Exception {
		this.documentGroupCreate();
	}

	public void onClick$menuitemDocumentGroupCreate(Event event)
			throws Exception {
		this.documentGroupCreate();
	}

	public void onClick$toolbarbuttonDocumentGroupEdit(Event event)
			throws Exception {
		this.documentGroupEdit();
	}

	public void onClick$menuitemDocumentGroupEdit(Event event) throws Exception {
		this.documentGroupEdit();
	}

	public void onClick$toolbarbuttonDocumentGroupDelete(Event event)
			throws Exception {
		this.documentGroupDelete();
	}

	public void onClick$menuitemDocumentGroupDelete(Event event)
			throws Exception {
		this.documentGroupDelete();
	}

	public void onClick$toolbarbuttonDocumentGroupSearch(Event event)
			throws Exception {
		this.documentGroupSearch();
	}

	public void onClick$menuitemDocumentGroupSearch(Event event)
			throws Exception {
		this.documentGroupSearch();
	}

	public void onClick$toolbarbuttonDocumentGroupRefresh(Event event)
			throws Exception {
		this.documentGroupRefresh();
	}

	public void onClick$menuitemDocumentGroupRefresh(Event event)
			throws Exception {
		this.documentGroupRefresh();
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);
		this.searchDocumentGroup = new Search(DocumentGroup.class);
		this.searchDocumentGroup.setMaxResults(100);
		this.searchDocumentGroup.addFetch("createdBy");
		this.documentGroups = this.documentGroupService
				.search(this.searchDocumentGroup);
		component.setAttribute("controller", this);
	}

	private void documentGroupCreate() throws Exception {
		this.execution.sendRedirect("~./zul/html/documentGroup.zul");
	}

	private void documentGroupEdit() throws Exception {
		if (this.selectedDocumentGroup != null) {
			this.execution.sendRedirect("~./zul/html/documentGroup.zul?Id="
					+ this.selectedDocumentGroup.getId().toString());
		}
	}

	private void documentGroupDelete() throws Exception {
		Set<Listitem> selectedItems = this.listboxDocumentGroup
				.getSelectedItems();

		if (selectedItems.size() > 0) {
			if (Messagebox.YES == Messagebox.show("Delete these records?",
					"Delete?", Messagebox.YES | Messagebox.NO,
					Messagebox.QUESTION)) {
				for (Listitem selectedItem : selectedItems) {
					DocumentGroup documentGroup = (DocumentGroup) selectedItem
							.getValue();
					this.documentGroupService.remove(documentGroup);
				}
				this.selectedDocumentGroup = null;
			}
		} else {
			this.message.setText("Please select at least one item!");
			this.message.setIcon("/img/exclamation.png");
		}
		this.documentGroups = this.documentGroupService
				.search(this.searchDocumentGroup);
		this.binder.loadAll();
	}

	private void documentGroupSearch() throws Exception {
		this.execution.setAttribute("searchDocumentGroup",
				this.searchDocumentGroup);

		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentGroupSearch.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			this.searchDocumentGroup = (Search) this.execution
					.getAttribute("searchDocumentGroup");
		}

		this.documentGroups = this.documentGroupService
				.search(this.searchDocumentGroup);
		this.binder.loadAll();
	}

	private void documentGroupRefresh() throws Exception {
		this.documentGroups = this.documentGroupService
				.search(this.searchDocumentGroup);
		this.binder.loadAll();
	}

}
